const { readFileName } = require("../utils/tool");
const { initSuperUser } = require("./user");

const keys = readFileName(__dirname);

function initModels() {
  let promises = [];
  keys.forEach((key) => {
    if(!key.startsWith("_")) {
      const { sequelize } = require("./" + key);
      sequelize && promises.push(sequelize.sync());
    }
  });
  return new Promise((resolve, reject) => {
    Promise.all(promises)
      .then((res) => {
        initSuperUser();
        resolve();
      })
      .catch((err) => {
        console.error(err);
        reject(err);
      });
  });
}

module.exports = {
  initModels,
};
